package com.amazon.commerce.service;

import org.apache.ws.security.WSPasswordCallback;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;

/**
 * A WSS4J password callback implementation.
 *
 * @author: Nik Van Looy
 * @since: 9/10/11
 */
public class PasswordCallback implements CallbackHandler {

    /** The password to use. */
    private String password;

    public PasswordCallback(String password) {
        this.password = password;
    }

    /** Handle the given callback. */
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof WSPasswordCallback) {
                WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
                pc.setPassword(password);
            } else {
                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
            }
        }
    }
}
